import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, IndexRoute, Redirect,hashHistory } from 'react-router';

require('es6-promise').polyfill();
require('isomorphic-fetch');

/*import url from './assets/aa.json';*/

export default class Login extends React.Component{
    constructor() {
        super()
        this.state = {
            useranameMessage:"",
            passwordMessage:"",
            mounted:false
        };
    }
    componentDidMount(){

        this.setState({
            mounted:true
        })
    }
    loginSubmit(){
        if(this.state.mounted){
            let addForm = ReactDOM.findDOMNode(this.refs.addForm);
            let username=addForm.querySelector('#username').value;
            let password=addForm.querySelector('#password').value;
            let flag=true;
            if( username == "" || username == null ){
                flag=false;
                this.setState({"useranameMessage":"用户名不能为空"})
            }else {
                this.setState({"useranameMessage":""})
            }
            if( password == "" || password == null ){
                flag=false;
                this.setState({"passwordMessage":"密码不能为空"})
            }else {
                this.setState({"passwordMessage":""})
            }
            if(flag){
                var formData = {};
                formData.username = username;
                formData.password = password;
                console.info(formData);
<<<<<<< HEAD
                fetch('./src/aa.json',
                    {
                        method: 'POST',
                        headers: {

                            'Accept': 'application/json',
                            "Content-Type": "application/json"
                        },
                        body: formData
                    }
                )
                    .then(function(response) {
=======

                fetch('./aa.json',{
                    method:"POST",
                    headers: {
                        'Accept': 'application/json',
                        "Content-Type": "application/json"
                    },
                    body:JSON.stringify(formData)
                }).then(function(response) {
>>>>>>> 4dfdfc729b10def885a89d6893053b7d5324b8c9
                         return response.json();
                    }).then(function(json) {
                        console.log(json);
                  /*  console.log('parsed json', json)*/
                }).catch(function(ex) {
                    console.log('parsing failed', ex)

                })
            }
        }

    }

     render(){
         return (
             <div className="loginInput-w">
                 <div ref="addForm"  className="loginInput">
                     <input className="input" id="username" type="text" placeholder="用户名"/>
                     <div className="tip">{this.state.useranameMessage}</div>
                     <input className="input" type="password" id="password" placeholder="密码"/>
                     <div className="tip">{this.state.passwordMessage}</div>
                     <button className="input login" onClick={this.loginSubmit.bind(this)}>登录</button>
                     <button className="input register"><Link to="/register" style={{color:"#3970d7",textDecoration:"none"}}>免费注册</Link></button>
                 </div>
             </div>
         );
     }
}

